	<script type="text/javascript">
	
	function deleteFile(obj)
	
	{
	
	
	if (window.XMLHttpRequest)
	
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	  
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    document.getElementById("BodyMain").removeChild(document.getElementById(obj));
	    document.getElementById("result").innerHTML="File Deleted Succesfully";
	    }
	  }
	
	xmlhttp.open("POST","deleteFile.php?delete="+obj,true);		  
	xmlhttp.send();
	}
	</script>
<?php

if(isset($_SESSION["clientupload"]) && $_SESSION["clientupload"] != "")
{


	$dirPath = $_SESSION["clientupload"]."/uploads/";

	echo "<div id=\"BodyMain\">";
	echo "<h2 class=\"header1\">".$_SESSION['clientname']."</h2>";


	//Make all definitions
	define('UP_DIR', $_SESSION['clientupload']."/uploads/");

	define('MAX_FILE_SIZE', 10240000);


	//Initialise flags

	$sizeOK  = false;

	$success = false;



	if(array_key_exists('upload', $_POST)) {



		//Replace any spaces in the original name for underscores

		$file = str_replace(' ', '_', $_FILES['file']['name']);



		//convert the max size to kb

		$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';







		//check it isn't too big

		if($_FILES['file']['size'] > 0 && $_FILES['file']['size'] <= MAX_FILE_SIZE) {

			$sizeOK = True;

		}



		if($sizeOK ) {

			//Lets see what the error level is

			switch($_FILES['file']['error']) {

				case 0:

					//Move file to folder and rename it
					$success = move_uploaded_file($_FILES['file']['tmp_name'], UP_DIR.$file);			



					//Make sure it was moved with no problems

					if($success) {

						$result = "$file uploaded successfully";

					}

					else {

						$result = "Error uploading $file. Please try again.";

					}

					break;

				case 2:	//we ignore error 1 because it is php.ini

					//File was too big!

					$result = "$file was too big to be uploaded. Please try a smaller file";

					break;

				case 4:

					//No file selected

					$result = "No file was selected, please go back and try again";

					break;

				default:

					$result = "System error uploading $file. Please contact a staff member to resolve this issue.";

			}

		}

		else {

			$result = "$file cannot be uploaded. Maximum size: $max. ";

		}

	}
		//If the form has been submitted, display the result

		if(isset($result) && $result!= "0") {

			echo("<br/><br/><p id=\"result\" ><b>$result</b></p>");

		}

?>

<br />
<br />
<br />
<br />
            <span class = "bodycopy center">
			<form action="" method="post" enctype="multipart/form-data" name="uploadFile">

				<label for="file">

					Upload File:

				</label>

				<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />

				<input type="file" name="file" id="file" />

				<input type="submit" name="upload" value="upload" />

			</form>
            </span><br/><br/>

<?php
	// open the specified directory and check if it's opened successfully
	if ($handle = opendir($dirPath)) {
           echo("<p><b>Uploaded Files:</b></p>");

	   // keep reading the directory entries 'til the end
	   while (false !== ($file = readdir($handle))) {

	      // just skip the reference to current and parent directory
	      if ($file != "." && $file != "..") {
	      
		 if (!is_dir("$dirPath/$file") && ! preg_match("/log/", $file)) {
		 
		    // found a file, do something with it?
            		echo "<span class=\"header2center\" id=\"".$file."\">".$file ."&nbsp;&nbsp;".round(filesize("$dirPath/$file")/1024,2)." KB &nbsp;&nbsp;".date ("F d Y H:i:s ", filemtime("$dirPath$file"))."<button value=\"$file\" name=\"delete\"  onclick=\"deleteFile('".$file."')\">Delete</button> </span><br />";
		    
		 }
	      }
	   }
	  
	   // ALWAYS remember to close what you opened
	   closedir($handle);
	}
?>

			</div>
			
<?php
	}
elseif(isset($_SESSION["clientupload"]) && $_SESSION["clientupload"] == "")
{
	echo "<h2 class=\"header1\">system has not been configured for you to upload files.</h2>";
}
else
{
	echo "<h2 class=\"header1\">Configuration Error.</h2>";
}

//Underbar Quickfix
echo "<div id=\"BodyMain\"> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/></div>";
?>
